#define _CRT_SECURE_NO_WARNINGS 1
class Solution {
public:
    const int MAX = 0x3f3f3f3f;
    int maxSubarraySumCircular(vector<int>& nums) {
        int n = nums.size();
        if (n == 1) return nums[0];
        vector<int> dpmax(n);
        auto dpmin = dpmax;
        int sum = nums[0];
        dpmax[0] = dpmin[0] = nums[0];
        int Max = -0x3f3f3f3f, Min = 0x3f3f3f3f;
        //cout<<nums[0]<<" ";
        for (int i = 1;i < n;i++)
        {
            sum += nums[i];
            dpmax[i] = max(dpmax[i - 1] + nums[i], nums[i]);
            dpmin[i] = min(dpmin[i - 1] + nums[i], nums[i]);
            Max = max(Max, dpmax[i]);
            Min = min(Min, dpmin[i]);
            //cout<<dpmin[i]<<" ";
        }
        // cout<<endl;
        // cout<<endl;
        //cout << Max << " " << sum << " " << Min << " " << sum - Min;
        int t = sum - Min;
        if (t == 0) t = -MAX;
        return max(Max, t);
    }
};